﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Razor;
using Think.Lenovo.Com.Models;
using Think.Lenovo.Com.Models.NewsModel;
using Think.Lenovo.Com.DataAccess;
using Think.Lenovo.Com.DataAccess.NewsInfo;
using Think.Lenovo.Com.Common;

namespace Think.Lenovo.Com.Controllers.News
{
    public class NewsController : Controller
    {
        public ActionResult ViewNewsAdd()
        {
            return View("/Views/News/ViewNewsAdd.cshtml");
        }

        public ActionResult ViewManageNews(int pageIndex, int pageSize)
        {
            NewsInfo n = new NewsInfo();
            #region//Step 1.得到列表的第一页
            LinkedList<NewsModel> list = n.GetViewNews(pageIndex, pageSize);
            ViewData["listNews"] = list;
            #endregion

            #region//Step 2. 得到分页页码
            int userCount = 0;//用户总数
            userCount = n.GetNewsCount();
            LinkedList<PageNumberModel> pages = PageBuilder.LinkedPage(userCount, pageSize, pageIndex, 10, "/News/ViewManageNews/{pageIndex}/" + pageSize, "{pageIndex}");
            ViewData["pagerNews"] = pages;
            #endregion

            return View("/Views/News/ViewNewsList.cshtml");
        }


        /// <summary>
        /// 添加新闻
        /// </summary>
        /// <param name="model">新闻实体</param>
        public ActionResult AddNews(NewsModel model)
        {
            NewsInfo u = new NewsInfo();
            long id = u.AddNews(model);
            JsonResult jr = new JsonResult();
            Dictionary<Object, Object> result = new Dictionary<object, object>();
            result.Add("isSuccess", id);
            jr.Data = result;
            return jr;
        }

        public ActionResult EditNews(NewsModel model)
        {
            NewsInfo u = new NewsInfo();
            long affect = u.UpdateNews(model);
            JsonResult jr = new JsonResult();
            Dictionary<Object, Object> result = new Dictionary<object, object>();
            result.Add("isSuccess", affect);
            jr.Data = result;
            return jr;
        }


        public ActionResult GetNewsById(int id)
        {
            NewsInfo u = new NewsInfo();
            NewsModel model = u.GetNewsInfo(id);
            JsonResult jr = new JsonResult();
            jr.Data = model;
            return jr;
        }
    }
}